<?php
/**
 * index
 */

namespace app\admin\controller;

use think\App;
use think\Controller;
use app\common\model\TplModule;
use app\common\model\TplModuleField;
use app\common\model\TplContent;

class Landing extends Controller
{

    public function __construct(App $app = null)
    {
        parent::__construct($app);

    }

    //landing page 操作页
    public function addcont($id){
        $TplModuleModel = new TplModule();

        $TplModuleList = $TplModuleModel->select();

        $TplContentModel = new TplContent();
        $TplContentList = $TplContentModel
            ->alias('c')
            ->leftJoin("TplModule m","m.id = c.house_id")
            ->where(['c.house_id'=>$id])
            ->field("c.*,m.module_name,m.module_tpl_url")
            ->order("c.sort desc")
            ->select();
        //var_dump($TplContentList);exit;

        $this->assign("TplContentList",$TplContentList);
        $this->assign("TplModuleList",$TplModuleList);
        return $this->fetch();
    }

    /**
     * 添加模板到落地页
     * @return \think\response\Json
     */
    public function addModuleToView(){
        try{
            $house_id = input('id');

            $tplModel = new TplContent();
            $data = [
                'type'=>2,
                'house_id'=>$house_id,
                'content'=>'',
                'create_time'=>time()
            ];

            $tplModel->insert($data);
            return json([
                'code' => 1,
                'msg'  => 'succ',
                'data' => '',
                'url'  => '',
                'wait' => '',
            ]);
        }catch (\Exception $e){
            return json([
                'code' => 0,
                'msg'  => $e->getMessage().$e->getLine().$e->getFile(),
                'data' => '',
                'url'  => '',
                'wait' => '',
            ]);
        }

    }

    public function edittplcont(){
        if (request()->isAjax()){
            var_dump(input());exit;

        }else{
            $id = input('id');
            $houseId = input('houseId');

            $TplModuleFieldModel = new TplModuleField();

            $TplModuleFieldList = $TplModuleFieldModel->where(['module_id'=>$houseId])->order("field_sort")->select();
            //var_dump($TplModuleFieldList->toArray());exit;

            $this->assign("id",$id);
            $this->assign("TplModuleFieldList",$TplModuleFieldList);
            return $this->fetch();
        }

    }

    /******************linding 操作*******************/


    //landing page 操作页
    public function index()
    {
        $pageModel = model("Page");

        $pageList = $pageModel->select();
        $this->assign("pageList",$pageList);
        return $this->fetch();
    }


    public function addlanding(){
        if (request()->isAjax()){
            try{
                $landing_name = input('landing_name');
                $landing_title = input('landing_title');
                $landing_keywords = input('landing_keywords');
                $landing_desc = input('landing_desc');
                $landing_url = input('landing_url');

                $tplModel = model("Page");
                $data = [
                    'landing_name'=>$landing_name,
                    'landing_title'=>$landing_title,
                    'landing_keywords'=>$landing_keywords,
                    'landing_desc'=>$landing_desc,
                    'landing_url'=>$landing_url,
                    'create_time'=>time()
                ];

                $tplModel->insert($data);
                return json([
                    'code' => 1,
                    'msg'  => "成功",
                    'data' => '',
                    'url'  => '',
                    'wait' => '',
                ]);
            }catch (\Exception $e){
                //return $this->error($e->getMessage().$e->getLine().$e->getFile());
                return json([
                    'code' => 0,
                    'msg'  => $e->getMessage().$e->getLine().$e->getFile(),
                    'data' => '',
                    'url'  => '',
                    'wait' => '',
                ]);
            }

        }else{
            return $this->fetch();
        }
    }
}